<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Moderate translations' );

global $language;
$language_info = translations_get_language_information( $language );

$query = "SELECT admin FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( ( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) || $language_info[ 'user_id' ] == $_SESSION[ 'user_id' ] ) {
	$type = 'approve';
	if( isset( $_GET[ 'type' ] ) && $_GET[ 'type' ] == 'edit' ) {
		$type = 'edit';
	}
	
	$filter = '';
	if( isset( $_GET[ 'filter' ] ) ) {
		$filter = $_GET[ 'filter' ];
	}
	$page = 1;
	if( isset( $_GET[ 'page' ] ) && is_numeric( $_GET[ 'page' ] ) ) {
		$page = $_GET[ 'page' ];
	}
	$per_page = 10;
	$end = $page * $per_page;
	
	if( $type == 'approve' ) {
		$max_page = ceil( $language_info[ 'pending' ] / $per_page );
		if( $end > $language_info[ 'pending' ] ) {
			$end = $language_info[ 'pending' ];
		}
	}
	else {
		$max_page = ceil( $language_info[ 'approved' ] / $per_page );
		if( $end > $language_info[ 'approved' ] ) {
			$end = $language_info[ 'approved' ];
		}
	}
?>
					<div class="blok">
						<a href="translate.php"><?php transprint( 'To language overview' ); ?></a>
					</div>
					<br />
					<div class="blok">
						<strong><?php transprint( 'Filter' ); ?></strong><br />
						<form method="get" action="moderate_translation.php">
							<input type="hidden" name="page" value="<?php print( $page );  ?>" />
							<input type="hidden" name="type" value="<?php print( $type );  ?>" />
							<input type="text" size="20" name="filter" value="<?php print( $filter );  ?>" />
							<input type="submit" value="<?php transprint( 'Filter' );  ?>" />
						</form>
					</div>
					<br />
<?php
	
	if( isset( $_SESSION[ 'translate_moderator_status' ] ) ) {
		print( "\t\t\t\t<div class=\"blok\">\r\n\t\t\t\t" . $_SESSION[ 'translate_moderator_status' ] . "\r\n\t\t\t\t</div><br />\r\n" );
		unset( $_SESSION[ 'translate_moderator_status' ] );
	}

	if( $max_page > 0 ) {
		if( $type == 'approve' ) {
			$count = ': ' . number_format( ( $page - 1 ) * $per_page ) . ' - ' . number_format( $end ) . ' out of ' . number_format( $language_info[ 'pending' ] );
		}
		else {
			$count = ': ' . number_format( ( $page - 1 ) * $per_page ) . ' - ' . number_format( $end ) . ' out of ' . number_format( $language_info[ 'approved' ] );
		}
?>
					<div class="blok translation-container">
						<strong><?php 
		if( $type == 'approve' ) {
			transprint( 'Awaiting approval' );
			$texts = translations_get_awaiting_approval( $language, $page, $per_page, $filter );
		}
		else {
			transprint( 'Translated' );
			$texts = translations_get_translated( $language, $page, $per_page, $filter );
		}
		if( $filter == '' ) {
			print( $count );
		} 
						?></strong><br />
<?php
		foreach( $texts AS $text ) {
			translations_print_translate_form( $text, 'submit_translation_moderator.php?type=' . $type, true );
			$query = "SELECT username " .
					"FROM users " .
					"WHERE user_id = " . $text[ 'user_id' ];
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			print( translate( 'Username' ) . ': ' . $row[ 'username' ] . '<br />' );
			$query2 = "SELECT name " .
					"FROM ruler " .
					"WHERE user_id = " . $text[ 'user_id' ];
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC);
			if( $row[ 'username' ] != 'Zworf' && $row[ 'username' ] != 'Zworf2' && isset( $row2[ 'name'] ) ) {
				print( translate( 'Ruler name' ) . ': ' . $row2[ 'name' ] );
			}
			else {
				print( translate( 'Ruler name' ) . ': ' . translate( 'unknown' ) );
			}
?>
						<br />
						<br />
<?php
		}
	
		if( $page > 1 ) {
?>
						<div id="text_left">
							<a href="moderate_translation.php?page=<?php print( ( $page - 1 ) . '&type=' . $type ); ?>&filter=<?php print( $filter ); ?>"><?php transprint( 'Previous' ) ?></a>
						</div>
<?php		
		}
	
		if( $page < $max_page ) {
?>
						<div id="text_right">
							<a href="moderate_translation.php?page=<?php print( ( $page + 1 ) . '&type=' . $type ); ?>&filter=<?php print( $filter ); ?>"><?php transprint( 'Next' ) ?></a>
						</div>
<?php		
		}
		
		if( $page < $max_page || $page > 1 ) {
			print( "<br />" );
		}
?>						
					</div>
<?php
	}
	else {
?>
					<div class="blok">
						<strong><?php transprint( 'No texts available' ); ?></strong><br />
						<?php transprint( 'There is either no text to approve or no translated texts to edit.' ); ?><br />
					</div>
<?php
	}
}
else {
?>
					<div class="blok">
						<strong><?php transprint( 'Access denied' ); ?></strong><br />
						<?php transprint( 'This page is only available to the language moderator and game admins.' ); ?>
					</div>
<?php
}
aos_footer();
?>